package my.suveng.oauth2.controller;

import my.suveng.model.common.interfaces.response.IMessage;
import my.suveng.model.common.response.element.ElementPage;
import my.suveng.model.common.response.element.PageQuery;
import my.suveng.oauth2.model.dto.*;
import my.suveng.oauth2.service.db.mysql.model.entity.SysPermissionRole;
import my.suveng.oauth2.service.db.mysql.model.entity.SysRole;
import my.suveng.oauth2.service.user.impl.RoleServiceImpl;
import my.suveng.oauth2.service.user.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理接口
 *
 * @author suwenguang
 **/
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private RoleServiceImpl roleService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 角色列表
     *
     * @author suwenguang
     */

    @PostMapping("/list")
    public IMessage<ElementPage<List<SysRole>>> roleList(@RequestBody PageQuery<RoleSearch> query) {
        return roleService.roleList(query);
    }

    /**
     * 增加角色
     *
     * @author suwenguang
     */

    @PostMapping(value = "/add")
    public IMessage<SysRole> add(@RequestBody RoleAddDto roleAddDto) {
        // 参数校验
        return userService.addRole(roleAddDto);
    }

    /**
     * 修改角色
     *
     * @author suwenguang
     */

    @PostMapping("/edit")
    public IMessage<SysRole> edit(@RequestBody RoleEditDto roleEditDto) {
        // 参数校验
        return roleService.edit(roleEditDto);
    }

    /**
     * 删除角色
     *
     * @author suwenguang
     */
    @PostMapping("/delete")

    public IMessage<Object> delete(@RequestBody RoleDeleteDto roleDeleteDto) {
        // 参数校验
        return roleService.delete(roleDeleteDto);
    }

    @PostMapping("/add/permission")
    public IMessage<List<SysPermissionRole>> addPermission(@RequestBody RoleAddPermissionDto roleAddPermissionDto) {
        // 参数校验
        return roleService.addPermissions(roleAddPermissionDto);
    }
}
